package com.orange.modules.dao;

import java.sql.SQLException;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.orange.dbhelp.DbHelper;
import com.orange.modules.entity.AuthSchoolRunningMap;
import com.orange.modules.entity.school_data.AuthSchoolRunningData;

@Repository
public class AuthSchoolRunningDataRepository {

	@Resource(name = "dbHelper")
	private DbHelper dbHelper;
	
	@Autowired
	private AuthSchoolRunningDataDao authSchoolRunningDataDao;
	
	@Autowired
	private AuthSchoolRunningMapDao authSchoolRunningMapDao;
	
	/**
	 * 保存办学数据、修改、添加
	 * @param asrd
	 * @throws SQLException
	 */
	public void insertAuthSchoolRunningData(AuthSchoolRunningData ard) throws SQLException{
		authSchoolRunningDataDao.save(ard);
	}	
	
	/**
	 * 删除教学子表数据
	 * @param srId
	 * @throws SQLException
	 */
	public void delAuthSchoolRunningData(String pid) throws SQLException{
		authSchoolRunningDataDao.delete(pid);
	}
	
	/**
	 * 根据办学数据id删除国家“卓越”计划 时间数据
	 * @param runningDataId
	 * @throws SQLException
	 */
	public void deleteAuthSchoolRunningMapBySRId(String runningDataId) throws SQLException{
		String sql="DELETE FROM t_auth_school_running_map WHERE sr_id=?";
		dbHelper.execute(sql, runningDataId);
	}
	
	/**
	 * 根据ID查询
	 * @param pid
	 * @return
	 * @throws SQLException
	 */
	public AuthSchoolRunningData queryAuthSchoolRunningDataById(String pid) throws SQLException{
		String sql = "SELECT s.p_id as pid,s.s_school_start_time as schoolStartTime,"
				+ "s.s_reorganization_time as reorganizationTime,"
				+ "s.s_school_upgraded_college_time as schoolUpgradedCollegeTime,"
				+ "s.s_undergraduate_school_time as undergraduateSchoolTime,"
				+ "s.s_undergraduate_professional_start_time as undergraduateProfessionalStartTime,"
				+ "s.s_specialty_startup_time as specialtyStartupTime,s.s_createtime as createtime,"
				+ "s.s_id as sid,s.login_type as loginType,s.s_status as status FROM t_auth_school_running_data AS s "
				+ "WHERE s.p_id = '"+pid+"'";
		AuthSchoolRunningData as=dbHelper.getBean(sql, AuthSchoolRunningData.class);
		return as;
	}
	
	
	/**
	 * 查询所有
	 * @return
	 * @throws SQLException
	 */
	public List<AuthSchoolRunningData> queryAuthSchoolRunningDataByAll() throws SQLException{
		List<AuthSchoolRunningData> as = (List<AuthSchoolRunningData>) authSchoolRunningDataDao.findAll();
		return as;		
	}
	
	
	
	
	
	
	
	
	
	/**
	 * 保存办学数据中的卓越计划时间
	 * @param asrm
	 * @throws SQLException
	 */
	public void insertRunningMap(AuthSchoolRunningMap asrm) throws SQLException{
		authSchoolRunningMapDao.save(asrm);
	}
	
	/**
	 * 根据学校ID 和 专业类型  查询
	 * @param schoolId
	 * @param loginType
	 * @return
	 * @throws SQLException
	 */
	public AuthSchoolRunningData queryAuthSchoolRunningDataBySchoolId(String schoolId,String loginType) throws SQLException{
		String sql = "SELECT t.p_id as pid, t.s_school_start_time as schoolStartTime,t.s_reorganization_time as reorganizationTime,"
				+ "t.s_school_upgraded_college_time as schoolUpgradedCollegeTime,t.s_undergraduate_school_time as undergraduateSchoolTime,"
				+ "t.s_undergraduate_professional_start_time as undergraduateProfessionalStartTime,"
				+ "t.s_specialty_startup_time as specialtyStartupTime ,t.s_createtime as createtime,t.s_id as sid ,t.login_type as loginType,"
				+ "t.s_status as status,t.s_remark as remark FROM t_auth_school_running_data t WHERE t.s_id =? ";
		if(StringUtils.isNotBlank(loginType)){
			sql+="AND  t.login_type ='"+loginType+"'";
		}
		AuthSchoolRunningData asrd = dbHelper.getBean(sql, AuthSchoolRunningData.class,schoolId);
		return asrd;
	}
	
	
	
	public List<AuthSchoolRunningMap> queryAuthSchoolRunningMapByItemId(String rdId) throws SQLException{
		String sql  = "SELECT t.p_id as pid,t.sr_id as srid,t.s_plantime as plantime,"
				+ "t.s_type as type FROM t_auth_school_running_map t WHERE t.sr_id ='"+rdId+"'";
		List<AuthSchoolRunningMap> asrm = dbHelper.getBeanList(sql, AuthSchoolRunningMap.class);
		return asrm;
	}
	
	
}
